package com.glory.brepda.activity.mfg;

import android.app.Activity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.glory.brepda.R;
import com.glory.brepda.adapter.DataBinListAdapter;
import com.glory.brepda.base.BaseActivity;
import com.glory.brepda.callback.CustomDialogEntityCallBack;
import com.glory.brepda.entity.AvailableProgramBean;
import com.glory.brepda.entity.BinInfoEntity;
import com.glory.brepda.entity.EqpEntity;
import com.glory.brepda.entity.LotInfoEntity;
import com.glory.brepda.entity.UpdateIPEntity;
import com.glory.brepda.entity.response.BaseResponseBean;
import com.glory.brepda.utils.LotUtils;
import com.glory.brepda.utils.NetUtils;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_CommonCallback;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralPop;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_DialogUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_LogUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_PopUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StaticMembers;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_ToastUtils;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentManager;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import org.json.JSONObject;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * FileName: FTStartTestActivity
 * Author: XJH
 * Date: 2020/6/13 17:27
 * Description: FT开测
 * History:
 */
public class FTStartTestActivity extends BaseActivity {


    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvBaseSubmit)
    TextView tvBaseSubmit;
    @BindView(R.id.etEqpWork)
    FreeUI_ClearEditText etEqpWork;
    @BindView(R.id.etEqp)
    FreeUI_ClearEditText etEqp;
    @BindView(R.id.etLot)
    FreeUI_ClearEditText etLot;
    @BindView(R.id.tvDataName)
    TextView tvDataName;
    @BindView(R.id.ivDataNameArrow)
    ImageView ivDataNameArrow;
    @BindView(R.id.flDataNameList)
    FrameLayout flDataNameList;
    @BindView(R.id.tvGrading)
    TextView tvGrading;
//    @BindView(R.id.tvEqpType)
//    TextView tvEqpType;
    @BindView(R.id.tvCustomerCode)
    TextView tvCustomerCode;
    @BindView(R.id.tvPartrrn)
    TextView tvPartrrn;
    @BindView(R.id.tvPackageType)
    TextView tvPackageType;
    @BindView(R.id.tvLotType)
    TextView tvLotType;
    @BindView(R.id.tvChipName)
    TextView tvChipName;
    @BindView(R.id.tvMaterialType)
    TextView tvMaterialType;
    @BindView(R.id.tvEqp)
    TextView tvEqp;
    @BindView(R.id.ivGradingArrow)
    ImageView ivGradingArrow;
    @BindView(R.id.flGradingList)
    FrameLayout flGradingList;
    @BindView(R.id.ivSITE)
    ImageView ivSITE;
    @BindView(R.id.flSITE)
    FrameLayout flSITE;
    @BindView(R.id.tvSITE)
    TextView tvSITE;
    @BindView(R.id.tvWoID)
    TextView tvWoID;
    @BindView(R.id.tvFileName)
    TextView tvFileName;
    @BindView(R.id.tvTestType)
    TextView tvTestType;
    @BindView(R.id.tvProgramgear)
    TextView tvProgramgear;
    @BindView(R.id.tvSite)
    TextView tvSite;
    @BindView(R.id.tvmSite)
    TextView tvmSite;
    @BindView(R.id.tvPartName)
    TextView tvPartName;
    @BindView(R.id.tvFileRemark)
    TextView tvFileRemark;
    @BindView(R.id.rvDataBinList)
    RecyclerView rvDataBinList;
    @BindView(R.id.tvcurrentlot)
    TextView tvcurrentlot;
    @BindView(R.id.etCurrentLot)
    FreeUI_ClearEditText etCurrentLot;
    @BindView(R.id.rvDataNameList)
    RecyclerView rvDataNameList;
    @BindView(R.id.rvGradingList)
    RecyclerView rvGradingList;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.llGetData)
    LinearLayout llGetData;
    @BindView(R.id.flStationIDList)
    FrameLayout flStationIDList;
    @BindView(R.id.tvStationID)
    TextView tvStationID;
    @BindView(R.id.ivStationIDArrow)
    ImageView ivStationIDArrow;
    private List<DataEntity> mDataNameList;
    private FreeUI_GeneralPop mDataNamePop;
    private DataBinListAdapter mDataBinListAdapter;
    //    private LotInfoEntity mLoInfoEntity = new LotInfoEntity();
    private FreeUI_GeneralPop mGradingPop;
    private FreeUI_GeneralPop mSITEPop;
    private FreeUI_GeneralPop mStationIDPop;
    private String mStationID = "";
    private List<BinInfoEntity.BinInfo> mRtData;
    private List<BinInfoEntity.BinInfo> mMtData;
    private DataNameAdapter mDataNameAdapter;
    private DataNameAdapter mGradingAdapter;
    private ArrayList<String> strings;
    private ArrayList<String> siteList = new ArrayList<>();
    private ArrayList<String> mGradingMakerArray;
    private ArrayList<String> stationidList = new ArrayList<>();
    private LotInfoEntity mLoInfoEntity = new LotInfoEntity();
    private AvailableProgramBean availableProgramBean;
    private String testblock = "0";
    private String location = "";

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_start_test_ft;
    }

    @Override
    protected void initView() {
        strings = new ArrayList<>();
        strings.add("FT(初测)");
        strings.add("RT(复测)");
        strings.add("MT(外观)");
        strings.add("CT(拆编)");
        strings.add("QA(标样)");
        strings.add("QC(EQC)");
        strings.add("YZ(验证)");
        strings.add("YP(样品)");
        strings.add("FT2(初测)_TS-2");
        strings.add("RT2(复测)_TS-2");
        strings.add("QA2(标样)_TS-2");
        strings.add("QC2(EQC)_TS-2");

        siteList.add("1");
        siteList.add("2");
        siteList.add("3");
        siteList.add("4");

        stationidList.add("A");
        stationidList.add("B");
        stationidList.add("C");
        stationidList.add("D");
        stationidList.add("ALL");

        mGradingMakerArray = new ArrayList<>();
        mGradingMakerArray.add("不分挡");
        mGradingMakerArray.add("A");
        mGradingMakerArray.add("B");
        mGradingMakerArray.add("C");
        mGradingMakerArray.add("D");
        mDataNameList = new ArrayList<>();
        mRtData = new ArrayList<>();
        mMtData = new ArrayList<>();
        setMainLayout(llMain);
        String title = getIntent().getStringExtra(FreeApi_StaticMembers.SHOW_PLUGIN_KEY);
        tvTitle.setText(title);
        tvGrading.setText("不分档");
        rvDataBinList.setLayoutManager(new GridLayoutManager(this, 2));
        mDataBinListAdapter = new DataBinListAdapter(null, true);
        mDataBinListAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        mDataBinListAdapter.isFirstOnly(true);
        rvDataBinList.setAdapter(mDataBinListAdapter);
        initRecyclerView(rvDataNameList, this);
        mDataNameAdapter = new DataNameAdapter(R.layout.item_ft_data_bin, mDataNameList, new DataNameAdapter.checkListener() {
            @Override
            public void onCheckListener(String codeS) {
                if (codeS.equals("RT(复测)") | codeS.equals("MT(外观)")) {
                    rvDataBinList.setVisibility(View.VISIBLE);
                    mRtData.clear();
                    mMtData.clear();
                    requestBin(codeS);
                }
                else {
                    rvDataBinList.setVisibility(View.GONE);
                    mDataBinListAdapter.setNewData(null);
                }
            }
        });
        initRecyclerView(rvGradingList, this);
        mGradingAdapter = new DataNameAdapter(R.layout.item_ft_data_bin, null, null);
        rvGradingList.setAdapter(mGradingAdapter);
        mDataNameAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        mDataNameAdapter.isFirstOnly(true);
        rvDataNameList.setAdapter(mDataNameAdapter);
        initEdit(etLot, s -> LotUtils.searchLotInfo(FTStartTestActivity.this, s, getSupportFragmentManager(), new LotUtils.netCallback<LotInfoEntity>() {
            @Override
            public void onSuccess(LotInfoEntity response) {
                mLoInfoEntity = response;
                tvLotType.setText(response.getPART().getSPEC2());
                tvCustomerCode.setText(response.getLOT().getCUSTOMERCODE());
                tvPartrrn.setText(response.getLOT().getCUSTOMERPARTID());
                tvPackageType.setText(response.getPACKAGETYPE());
                tvMaterialType.setText(response.getLOT().getPARTTYPE());
                tvChipName.setText(response.getCHIPNAME());
                tvPartName.setText(response.getLOT().getPARTNAME());
                tvWoID.setText(response.getLOT().getWOID());
            }

            @Override
            public void onFail(Throwable t) {
                loadError(t, "LOTQUERY");
            }
        }));


        initEdit(etEqpWork, new editTextListener() {
            @Override
            public void onTextChangeListener(String s) {
                FreeUI_EntityCallBack<BaseResponseBean<EqpEntity>> callBack = new CustomDialogEntityCallBack<BaseResponseBean<EqpEntity>>
                        (new TypeToken<BaseResponseBean<EqpEntity>>() {
                        }.getType(), getSupportFragmentManager(), this) {

                    @Override
                    public void onSuccess(Response<BaseResponseBean<EqpEntity>> response) {
                        super.onSuccess(response);
                        if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                            EqpEntity eqpEntity = response.body().getResponse().getBody();
                            location = eqpEntity.getEquipment().getLocation();
                        }
                    }

                    @Override
                    public void onError(Response<BaseResponseBean<EqpEntity>> response) {
                        super.onError(response);
                        loadError(response.getException(), "EQUIPMENTID");
                    }
                };

                JsonObject map = new JsonObject();
                map.addProperty("ACTIONTYPE", "EQPBYID");
                EqpEntity.EquipmentlistBean equipmentlistBean = new EqpEntity.EquipmentlistBean();
                equipmentlistBean.setEquipmentid(s);
                map.add("EQUIPMENT", new JsonParser().parse(new Gson().toJson(equipmentlistBean)).getAsJsonObject());
                NetUtils.requestPostNet("IF.BREQUERYEQP", "Common", FTStartTestActivity.this, map, callBack);
            }
        });

        initEdit(etEqp, new editTextListener() {
            @Override
            public void onTextChangeListener(String s) {
                FreeUI_EntityCallBack<BaseResponseBean<UpdateIPEntity>> callBack = new CustomDialogEntityCallBack<BaseResponseBean<UpdateIPEntity>>
                        (new TypeToken<BaseResponseBean<UpdateIPEntity>>() {
                        }.getType(), getSupportFragmentManager(), this) {

                    @Override
                    public void onSuccess(Response<BaseResponseBean<UpdateIPEntity>> response) {
                        super.onSuccess(response);
                        if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {

                            UpdateIPEntity body = response.body().getResponse().getBody();
                            FreeApi_LogUtils.saveErrorMessages(body.toString(),"AAA");

                            tvEqp.setText(body.getPROGRAMTYPE());
//                            tvSITE.setText(body.getSITE());
                            tvmSite.setText(body.getSiteNo());
//                            tvTestType.setText(body.getPROGRAMTYPE());
                        }
                    }

                    @Override
                    public void onError(Response<BaseResponseBean<UpdateIPEntity>> response) {
                        super.onError(response);
                        loadError(response.getException(), "EQUIPMENTID");
                    }
                };

                JsonObject map = new JsonObject();
                map.addProperty("EQUIPMENTID", s);
                NetUtils.requestPostNet("IF.GetEquipment", "GetEquipment", FTStartTestActivity.this, map, callBack);

            }
        });

        initEdit(etCurrentLot, s -> LotUtils.searchLotInfo(FTStartTestActivity.this, s, getSupportFragmentManager(), new LotUtils.netCallback<LotInfoEntity>() {
            @Override
            public void onSuccess(LotInfoEntity response) {

            }

            @Override
            public void onFail(Throwable t) {
                loadError(t, "LOTQUERY");
            }
        }));
    }

    @Override
    protected void initData() {
        tvcurrentlot.setVisibility(View.GONE);
        etCurrentLot.setVisibility(View.GONE);
    }


    @OnClick({R.id.tvBaseSubmit, R.id.flDataNameList, R.id.flGradingList, R.id.llGetData, R.id.flSITE, R.id.flStationIDList})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tvBaseSubmit:
//                if (actionControl("Submit27")) return;
                String grading;
                if (rvGradingList.getVisibility() == View.GONE) {
                    grading = "不分档";
                } else {
                    if (mGradingAdapter.getDataName().equals("")) {
                        showToast("请选择档位", false);
                        return;
                    }

                    grading = mGradingAdapter.getDataName();
                }


                if (getText(etEqpWork).equals("")) {
                    showToast("请先扫描分选机ID", false);
                } else if (mStationID.equals("")) {
                    showToast("请选择站别", false);
                } else if (tvDataName.getText().toString().equals("")) {
                    showToast("请选择测试类型", false);
                } else if (availableProgramBean == null) {
                    showToast("请先查询程序信息", false);
                } else {
                    testblock = "";
                    switch (tvGrading.getText().toString()) {
                        case "不分档":
                            testblock = "0";
                            break;
                        case "A":
                            testblock = "1";
                            break;
                        case "B":
                            testblock = "2";
                            break;
                        case "C":
                            testblock = "3";
                            break;
                        case "D":
                            testblock = "4";
                            break;

                    }
                    FreeApi_DialogUtils.getConfirmAndCancelDialog(FTStartTestActivity.this, "确定要提交?", new FreeUI_CommonCallback() {
                        @Override
                        public void onSuccess() {
                            //确定
                            FreeUI_EntityCallBack<BaseResponseBean<JSONObject>> callBack = new CustomDialogEntityCallBack<BaseResponseBean<JSONObject>>
                                    (new TypeToken<BaseResponseBean<JSONObject>>() {
                                    }.getType(), getSupportFragmentManager(), FTStartTestActivity.this) {

                                @Override
                                public void onSuccess(Response<BaseResponseBean<JSONObject>> response) {
                                    super.onSuccess(response);
                                    if (response.body().isSuccess(FTStartTestActivity.this, false, getSupportFragmentManager())) {
                                        FreeApi_ToastUtils.showFreeToast("提交成功", FTStartTestActivity.this, true, 0);
                                        FTStartTestActivity.this.finish();
                                    }
                                }

                                @Override
                                public void onError(Response<BaseResponseBean<JSONObject>> response) {
                                    super.onError(response);
                                    FreeApi_LogUtils.saveErrorMessages(response.getException(), "STARTTESTSUBMIT");
                                    FreeApi_ToastUtils.showFreeToast("连接服务器失败", FTStartTestActivity.this, false, 0);
                                }
                            };
                            JsonObject map = new JsonObject();
                            map.addProperty("ACTIONTYPE", "Test");
                            map.addProperty("TESTTYPE", tvDataName.getText().toString().contains("_")?tvDataName.getText().toString().substring(0,3)+"_"+tvDataName.getText().toString().split("_")[1]:tvDataName.getText().toString().substring(0, 2));
                            map.addProperty("LOTID",  getText(etLot));
                            map.addProperty("WOID",mLoInfoEntity.getLOT().getWOID());
                            map.addProperty("PRODUCTMODEL", mLoInfoEntity.getPRODUCTMODEL());
                            map.addProperty("WIRINGNO", mLoInfoEntity.getWIRINGNO());
                            map.addProperty("SEALNO", mLoInfoEntity.getSEALNO());
                            map.addProperty("CHIPNAME", mLoInfoEntity.getCHIPNAME());
                            map.addProperty("WIRESPEC", mLoInfoEntity.getWIRESPEC());
                            map.addProperty("LINEID", mLoInfoEntity.getLINEID());
                            map.addProperty("EQUIPMENTID", getText(etEqpWork));
                            map.addProperty("CHIPLOT", mLoInfoEntity.getCHIPID());
                            map.addProperty("LOTTYPE", mLoInfoEntity.getLOT().getLOTTYPE());
                            map.addProperty("PACKAGETYPE", mLoInfoEntity.getLOT().getMAINMATTYPE());
                            map.addProperty("CUSTOMERCODE", mLoInfoEntity.getLOT().getCUSTOMERCODE());
                            map.addProperty("LOTCOUNT", mLoInfoEntity.getLOT().getMAINQTY());
                            map.addProperty("SPC2", mLoInfoEntity.getPART().getSPEC2());
                            map.addProperty("GRADENO", grading.equals("不分档") ? "0" : grading);
                            map.addProperty("LOCATION", location);
                            map.addProperty("EQPTYPE", "");
                            map.addProperty("TESTERIDS", getText(etEqp));
                            map.addProperty("StationID", mStationID);
                            map.addProperty("TESTBLOCK", testblock);
                            map.addProperty("GradeNo", availableProgramBean.getGradeno());
                            map.addProperty("CHIPNAME", mLoInfoEntity.getCHIPNAME());
                            map.addProperty("OBJECTRRN", mLoInfoEntity.getLOT().getOBJECTRRN());
                            map.addProperty("DESCRIPTION", mLoInfoEntity.getLOT().getPARTDESC());
                            map.addProperty("PARTNAME", mLoInfoEntity.getLOT().getPARTNAME());
                            map.addProperty("PARTTYPE", mLoInfoEntity.getLOT().getPARTTYPE());
                            map.addProperty("CONNECTIONSTATE", "ONLINE");
                            map.addProperty("ProductType", mLoInfoEntity.getLOT().getMATERIALTYPE());
                            map.addProperty("FileUrl",availableProgramBean.getFileUrl());
                            map.addProperty("FileRemark", availableProgramBean.getFileRemark());
                            map.addProperty("FileName", availableProgramBean.getFileName());
                            map.addProperty("SiteNo", availableProgramBean.getSiteNo());
                            map.addProperty("ID", availableProgramBean.getID());
                            map.addProperty("TestType", availableProgramBean.getTestType());
                            map.addProperty("ProgramGear", availableProgramBean.getProgramGear());
                            map.addProperty("TOTALLSL", mLoInfoEntity.getTOTALLSL());
                            map.addProperty("CURRENTLOT",getText(etCurrentLot));
                            map.addProperty("ORDERID",mLoInfoEntity.getWORKORDER().getCUSTOMERORDER());
                            map.add("HBINLIST", new JsonParser().parse(new Gson().toJson( mDataBinListAdapter.getItemWork())).getAsJsonArray());
                            NetUtils.requestPostNet("STARTTESTSUBMIT", "STARTTESTSUBMIT", FTStartTestActivity.this, map, callBack);

                        }
                    }, () -> {

                    }).show(getSupportFragmentManager(), "SUBMIT_DIALOG");

                    /*LotUtils.binReport(this, getSupportFragmentManager(), "Test", tvDataName.getText().toString().substring(0, 2),
                            getText(etLot), getText(etEqpWork), mLoInfoEntity, mDataBinListAdapter.getItemWork(), grading.equals("不分档") ? "0" : grading,
                            "", getText(etEqp), mStationID, "STARTTESTSUBMIT", "ONLINE", availableProgramBean, testblock,location);*/
                }
                break;
            case R.id.flDataNameList:
                if (mDataNamePop != null) {
                    if (mDataNamePop.isShowing()) {
                        mDataNamePop.dismiss();
                    }
                } else {
                    mDataNamePop = FreeApi_PopUtils.getNormalPop(getApplicationContext(), flDataNameList.getWidth(), strings, ivDataNameArrow, (codeS, i) -> {
                        if (codeS.equals("RT(复测)") | codeS.equals("MT(外观)")) {
                            rvDataBinList.setVisibility(View.VISIBLE);
                            mRtData.clear();
                            mMtData.clear();
                            requestBin(codeS);
                        } else if (codeS.equals("QA(标样)")) {
                            rvDataBinList.setVisibility(View.GONE);
                            mDataBinListAdapter.setNewData(null);
                            tvcurrentlot.setVisibility(View.VISIBLE);
                            etCurrentLot.setVisibility(View.VISIBLE);
                        } else {
                            rvDataBinList.setVisibility(View.GONE);
                            mDataBinListAdapter.setNewData(null);

                            tvcurrentlot.setVisibility(View.GONE);
                            etCurrentLot.setVisibility(View.GONE);
                        }
                        tvDataName.setText(codeS);
//                        llBin7_8.setVisibility(View.GONE);
//                        llBin1_6.setVisibility(View.GONE);
//                        if (codeS.equals("RT(复测)")){
//                            llBin7_8.setVisibility(View.GONE);
//                            llBin1_6.setVisibility(View.VISIBLE);
//                        }
//                        if (codeS.equals("MT(外观)")){
//                            llBin7_8.setVisibility(View.VISIBLE);
//                            llBin1_6.setVisibility(View.GONE);
//                        }
                    });
                }
                FreeApi_PopUtils.setArrowShow(ivDataNameArrow);
                mDataNamePop.showAsDropDown(flDataNameList, 0, 0);
                break;
            case R.id.flGradingList:
//                if (mGradingMaker.equals("") || mGradingMaker.equals("不分档")){
//                    return;
//                }
                if (mGradingPop != null) {
                    if (mGradingPop.isShowing()) {
                        mGradingPop.dismiss();
                    }
                } else {

                    mGradingPop = FreeApi_PopUtils.getNormalPop(getApplicationContext(), flGradingList.getWidth(), mGradingMakerArray, ivGradingArrow, (codeS, i) -> {
                        tvGrading.setText(codeS);
                    });
                }
                FreeApi_PopUtils.setArrowShow(ivGradingArrow);
                mGradingPop.showAsDropDown(flGradingList, 0, 0);
                break;
            case R.id.flStationIDList:
                if (mStationIDPop != null) {
                    if (mStationIDPop.isShowing()) {
                        mStationIDPop.dismiss();
                    }
                } else {

                    mStationIDPop = FreeApi_PopUtils.getNormalPop(getApplicationContext(), flStationIDList.getWidth(), stationidList, ivStationIDArrow, (codeS, i) -> {
                        tvStationID.setText(codeS);
                        mStationID = codeS;
                    });
                }
                FreeApi_PopUtils.setArrowShow(ivStationIDArrow);
                mStationIDPop.showAsDropDown(flStationIDList, 0, 0);
                break;
            case R.id.flSITE:
                if (mSITEPop != null) {
                    if (mSITEPop.isShowing()) {
                        mSITEPop.dismiss();
                    }
                } else {
                    mSITEPop = FreeApi_PopUtils.getNormalPop(getApplicationContext(), flSITE.getWidth(), siteList, ivSITE, (codeS, i) -> {
                        tvSITE.setText(codeS);
                    });
                }
                FreeApi_PopUtils.setArrowShow(ivSITE);
                mSITEPop.showAsDropDown(flSITE, 0, 0);
                break;
            case R.id.llGetData:

                switch (tvGrading.getText().toString()) {
                    case "不分档":
                        testblock = "0";
                        break;
                    case "A":
                        testblock = "1";
                        break;
                    case "B":
                        testblock = "2";
                        break;
                    case "C":
                        testblock = "3";
                        break;
                    case "D":
                        testblock = "4";
                        break;

                }

                if ("".equals(tvDataName.getText().toString())){
                    showToast("请选择测试类型",false);
                    return;
                }
                FreeUI_EntityCallBack<BaseResponseBean<AvailableProgramBean>> callBack = new CustomDialogEntityCallBack<BaseResponseBean<AvailableProgramBean>>
                        (new TypeToken<BaseResponseBean<AvailableProgramBean>>() {
                        }.getType(), getSupportFragmentManager(), this) {

                    @Override
                    public void onSuccess(Response<BaseResponseBean<AvailableProgramBean>> response) {
                        super.onSuccess(response);
                        if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                            availableProgramBean = response.body().getResponse().getBody();
                            tvFileName.setText(availableProgramBean.getFileName());
                            tvTestType.setText(availableProgramBean.getTestType());
                            tvProgramgear.setText(availableProgramBean.getGradeno());
                            tvFileRemark.setText(availableProgramBean.getFileRemark());
                        }
                    }

                    @Override
                    public void onError(Response<BaseResponseBean<AvailableProgramBean>> response) {
                        super.onError(response);
                        loadError(response.getException(), "EQUIPMENTID");
                    }
                };
                FreeApi_LogUtils.saveErrorMessages(tvmSite.getText().toString(),"AAA");

                JsonObject map = new JsonObject();
                map.addProperty("CustomerCode", tvCustomerCode.getText().toString());
                map.addProperty("ProductCode", mLoInfoEntity.getLOT().getPARTNAME());
                map.addProperty("CustomerType", tvPartrrn.getText().toString());
                map.addProperty("LineId", mLoInfoEntity.getLOT().getWOID());
                map.addProperty("TestType", tvDataName.getText().toString().contains("_")?tvDataName.getText().toString().substring(0,3)+"_"+tvDataName.getText().toString().split("_")[1]:tvDataName.getText().toString().substring(0, 2));
                map.addProperty("ProgramGear", testblock);
                map.addProperty("LotId", getText(etLot));
                map.addProperty("SiteNo", tvmSite.getText().toString());
                map.addProperty("ChipName", tvChipName.getText().toString());
                map.addProperty("ProgramPlatform", tvEqp.getText().toString());
                NetUtils.requestPostNet("IF.GetAvailableProgram", "GetAvailableProgram", FTStartTestActivity.this, map, callBack);
                break;
        }
    }

    private List<DataEntity> getGradingMakerList(String mGradingMaker) {
        List<DataEntity> mGradingList = new ArrayList<>();
        for (int i = 1; i <= Integer.parseInt(mGradingMaker); i++) {
            mGradingList.add(new DataEntity(String.valueOf(i)));
        }
        return mGradingList;
    }

    private void requestBin(String testType) {
        mMtData.add(new BinInfoEntity.BinInfo("BIN7", ""));
        mMtData.add(new BinInfoEntity.BinInfo("BIN8", ""));
        mRtData.add(new BinInfoEntity.BinInfo("BIN1", ""));
        mRtData.add(new BinInfoEntity.BinInfo("BIN2", ""));
        mRtData.add(new BinInfoEntity.BinInfo("BIN3", ""));
        mRtData.add(new BinInfoEntity.BinInfo("BIN4", ""));
        mRtData.add(new BinInfoEntity.BinInfo("BIN6", ""));
        mDataBinListAdapter.getItemWork().clear();
        mDataBinListAdapter.setNewData(null);
        mDataBinListAdapter.setNewData(testType.equals("RT(复测)") ? mRtData : mMtData);
    }

    public static class DataNameAdapter extends BaseQuickAdapter<DataEntity, BaseViewHolder> {
        private int index;
        private String dataName = "";
        private checkListener listener;

        public DataNameAdapter(int layoutResId, @Nullable List<DataEntity> data, checkListener listener) {
            super(layoutResId, data);
            this.listener = listener;
            index = -1;
        }


        @Override
        protected void convert(BaseViewHolder helper, DataEntity item) {
            helper.setText(R.id.checkbox, item.getName());
            CheckBox checkBox = helper.getView(R.id.checkbox);
            checkBox.setChecked(item.isChecked());
            int adapterPosition = helper.getAdapterPosition();
            checkBox.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (index == -1) {
                        index = adapterPosition;
                        dataName = checkBox.getText().toString();
                        item.setChecked(true);
                    } else if (adapterPosition == index) {
                        dataName = "";
                        index = -1;
                        item.setChecked(false);
                    } else if (adapterPosition != index) {
                        getData().get(index).setChecked(false);
                        index = adapterPosition;
                        item.setChecked(true);
                        dataName = checkBox.getText().toString();
                    }

                    if (listener != null) {
                        listener.onCheckListener(dataName.split("/")[0]);
                    }
                    notifyDataSetChanged();
                }
            });
        }

        public String getDataName() {
            return dataName == null ? "" : dataName;
        }

        public interface checkListener {
            void onCheckListener(String code);
        }
    }

    public static class DataEntity implements Serializable {
        private String name;
        private boolean checked;

        public DataEntity(String name) {
            this.name = name;
        }

        public String getName() {
            return name == null ? "" : name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public boolean isChecked() {
            return checked;
        }

        public void setChecked(boolean checked) {
            this.checked = checked;
        }
    }
}
